package Arrays;

public class _189_RotateArray {
    public void rotate(int[] nums, int k) {
        int length = nums.length;
        if(length==0 ||k==0)  return;
        k = k%length;
        int[] temp = new int[length];
        for(int i=0;i<length;i++){
            temp[(i+k)%length] = nums[i];
        }
        for(int i=0;i<length;i++){
            nums[i] = temp[i];
        }
    }

    //更快的解法
    public void rotate1(int[] nums, int k) {
        if (k > nums.length)
            k = k % nums.length;
        int[] result = new int[nums.length];
        System.arraycopy(nums, nums.length - k, result, 0, k);
        System.arraycopy(nums, 0, result, k, nums.length - k);
        System.arraycopy(result, 0, nums, 0, nums.length);
    }
}
